package com.eling.elcms.cater.service.impl;

import com.eling.elcms.basedoc.model.PersonalInfo;
import com.eling.elcms.cater.dao.IConsumeCardDao;
import com.eling.elcms.cater.model.ConsumeCard;
import com.eling.elcms.cater.model.TravelAccount;
import com.eling.elcms.cater.model.TravelAccount.AccountType;
import com.eling.elcms.cater.model.TravelAccountFlow;
import com.eling.elcms.cater.model.TravelAccountFlow.ConsumeType;
import com.eling.elcms.cater.service.IConsumeCardService;
import com.eling.elcms.cater.service.ITravelAccountFlowService;
import com.eling.elcms.cater.service.ITravelAccountService;
import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.exception.BusinessException;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.travel.model.RoomPersonalInfo;
import com.eling.elcms.travel.model.WelfareConsumptionCoupon.Status;
import com.eling.elcms.travel.service.IRoomPersonalInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConsumeCardServiceImpl extends GenericManagerImpl<ConsumeCard, Long> implements IConsumeCardService {

	IConsumeCardDao conCardBindRecordDao;

	@Autowired
	private IRoomPersonalInfoManager roomPersonalInfoManager;

	@Autowired
	private ITravelAccountService travelAccountService;

	@Autowired
	private ITravelAccountFlowService travelAccountFlowService;

	@Autowired
	public void setConsumeCardDao(IConsumeCardDao conCardBindRecordDao) {
		this.dao = conCardBindRecordDao;
		this.conCardBindRecordDao = conCardBindRecordDao;
	}

	@Override
	public ConsumeCard grantCard(ConsumeCard cond, PersonalInfo personalInfo, TravelAccount travelAccount) {
		ConsumeCard card = new ConsumeCard();
		card.setCardNumber(cond.getCardNumber());
		List<ConsumeCard> cardList = query(card);
		// 查找卡
		if (cardList.isEmpty()) {
			throw new BusinessException("此卡不存在！");
		} else {
			card = cardList.get(0);
			if(card.getStatus().equals(com.eling.elcms.cater.model.ConsumeCard.Status.InUse)){
				throw new BusinessException("该卡已被绑定！");
			}else{
				card.setStatus(com.eling.elcms.cater.model.ConsumeCard.Status.InUse);
				card = save(card);
			}
		}
		RoomPersonalInfo roomPersonalInfo = new RoomPersonalInfo();
		PropertyUtils.setProperty(roomPersonalInfo, "personalInfo.name", personalInfo.getName());
		PropertyUtils.setProperty(roomPersonalInfo, "personalInfo.idNumber", personalInfo.getIdNumber());
		List<RoomPersonalInfo> perInfoList = roomPersonalInfoManager.query(roomPersonalInfo);
		// 查找入住人
		if(perInfoList.isEmpty()){
			throw new BusinessException("该客户不存在或未入住！");
		} else {
			roomPersonalInfo = perInfoList.get(0);
		}
		roomPersonalInfo.setConsumeCard(card);
		roomPersonalInfo = roomPersonalInfoManager.save(roomPersonalInfo);
		TravelAccount account = new TravelAccount();
		account.setRoomPersonalInfo(roomPersonalInfo);
		List<TravelAccount> accountList = travelAccountService.query(account);
		// 判断是否需要新增帐户
		Double balance = travelAccount.getBalance();
		if(!accountList.isEmpty()){
			account = accountList.get(0);
			account.setBalance(balance);
			account.setStatus(Status.Effective);
			account.setCreatDate(travelAccount.getCreatDate());
			account = travelAccountService.save(account);
		}else{
			account.setRoomPersonalInfo(roomPersonalInfo);
			account.setBalance(balance);
			account.setStatus(Status.Effective);
			account.setAccountType(AccountType.Consumer);
			account.setCreatDate(travelAccount.getCreatDate());
			account = travelAccountService.save(account);
		}
		// 创建流水
		TravelAccountFlow accountFlow = new TravelAccountFlow();
		accountFlow.setBalance(balance);
		accountFlow.setTravelAccount(account);
		accountFlow.setConsumeType(ConsumeType.Recharge);
		accountFlow.setMoney(balance);
		accountFlow.setOperateDate(travelAccount.getCreatDate());
		accountFlow.setServicePoint(cond.getServicePoint());
		accountFlow.setOperator((CommonUser)AppContext.curUser());
		travelAccountFlowService.save(accountFlow);
		return new ConsumeCard();
	}

	@Override
	public ConsumeCard loss(TravelAccountFlow cond) {
		// 修改帐户状态为挂失
		cond.getTravelAccount().setStatus(Status.Lose);
		cond = travelAccountFlowService.save(cond);
		// 修改卡状态为空闲
		ConsumeCard card = cond.getTravelAccount().getRoomPersonalInfo().getConsumeCard();
		card.setStatus(com.eling.elcms.cater.model.ConsumeCard.Status.Free);
		save(card);
		return new ConsumeCard();
	}

	@Override
	public ConsumeCard reissue(TravelAccountFlow cond) {
		ConsumeCard card = new ConsumeCard();
		card.setCardNumber(cond.getTravelAccount().getRoomPersonalInfo().getConsumeCard().getCardNumber());
		List<ConsumeCard> cardList = query(card);
		// 查找卡
		if (cardList.isEmpty()) {
			throw new BusinessException("此卡不存在！");
		} else {
			card = cardList.get(0);
			if(card.getStatus().equals(com.eling.elcms.cater.model.ConsumeCard.Status.InUse)){
				throw new BusinessException("该卡已被绑定！");
			}else{
				card.setStatus(com.eling.elcms.cater.model.ConsumeCard.Status.InUse);
				card = save(card);
			}
		}
		// 获取帐户
		TravelAccount account = travelAccountService.get(cond.getTravelAccount().getPkTravelAccount());
		RoomPersonalInfo roomPersonalInfo = account.getRoomPersonalInfo();
		ConsumeCard oldCard = roomPersonalInfo.getConsumeCard();
		oldCard.setStatus(com.eling.elcms.cater.model.ConsumeCard.Status.Free);
		save(oldCard);
		roomPersonalInfo.setConsumeCard(card);
		roomPersonalInfoManager.save(roomPersonalInfo);
		// 创建流水,还原帐户状态为有效
		account.setStatus(Status.Effective);
		cond.setTravelAccount(account);
		travelAccountFlowService.save(cond);
		return new ConsumeCard();
	}

	@Override
	public ConsumeCard recharge(TravelAccountFlow cond) {
		TravelAccount account = travelAccountService.get(cond.getTravelAccount().getPkTravelAccount());
		Double balance = account.getBalance();
		if(balance == null){
			balance = 0.0;
		}
		balance = cond.getMoney() + balance;
		account.setBalance(balance);
		cond.setTravelAccount(account);
		cond.setBalance(balance);
		travelAccountFlowService.save(cond);
		return new ConsumeCard();
	}

	@Override
	public ConsumeCard returned(TravelAccountFlow cond) {
		TravelAccount account = travelAccountService.get(cond.getTravelAccount().getPkTravelAccount());
		Double balance = account.getBalance();
		if(balance == null){
			balance = 0.0;
		}
		balance = balance - cond.getMoney();
		account.setBalance(balance);
		account.setStatus(Status.Returned);
		cond.setTravelAccount(account);
		cond.setBalance(balance);
		travelAccountFlowService.save(cond);
		ConsumeCard card = account.getRoomPersonalInfo().getConsumeCard();
		card.setStatus(com.eling.elcms.cater.model.ConsumeCard.Status.Free);
		save(card);
		return new ConsumeCard();
	}
}
